package com.gtm.service;

import com.gtm.model.Proposal;
import com.gtm.model.Topic;
import com.gtm.model.Translation;
import com.gtm.model.User;
import com.gtm.repository.ProposalRepository;
import com.gtm.repository.TopicRepository;
import com.gtm.repository.UserRepository;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProposalService extends BaseService<Proposal>{
    @Autowired
    MongoTemplate mt;

    @Autowired
    private ProposalRepository ppr;

    @Autowired
    UserRepository usr;

    @Autowired
    TopicRepository tpr;

    public Proposal uploadProposal(String sid, String summary, String keys, String references, MultipartFile reviewFile) throws IOException {
        User user = usr.findOne(sid);
        if(user.getAdditionalData()==null||user.getAdditionalData().get("state")==null)
            return null;
        String state = user.getAdditionalData().get("state").toString();
        List<String> stlist=new ArrayList<String>();
        stlist.add("TRANSLATION_PASS");stlist.add("PROPOSAL_PENDING");
        if(stlist.contains(state)==false)
            return null;

        Proposal pp=null;
        if(ppr.findByStuId(sid)!=null) {
            pp = ppr.findByStuId(sid);
        }
        else {
            pp=new Proposal();
            pp.setStuId(sid);
        }

        String tid=user.getTopicId();
        String sname=user.getRealname();
        pp.setStuName(sname);
        pp.setTopicId(tid);
        pp.setSummary(summary);
        String refile=upUserFile(reviewFile,pp);
        DBObject ref=new BasicDBObject();
        ref.put("proposalDoc",refile);
        ref.put("uploadTime",new Date());
        List<DBObject> relist=new ArrayList<DBObject>();
        if(null!= pp.getProsalFile())
            relist= pp.getProsalFile();
        relist.add(ref);
        pp.setProsalFile(relist);
        Topic tpc=tpr.findOne(tid);
        String topicNmae=tpc.getTopicName();
        String teacherName=tpc.getTeacherName();
        String teacherId=tpc.getTeacherId();
        pp.setTopicName(topicNmae);
        pp.setTeacherId(teacherId);
        pp.setTeacherName(teacherName);
        user.setAdditionalData("state","PROPOSAL_PENDING");
        user.setAdditionalData("proposalId",ppr.save(pp).getId());
        usr.save(user);
        return null;
    }

    public Proposal passProposal(String rid, String comment) {
        Proposal pp=new Proposal();
        pp=ppr.findOne(rid);
        pp.setState("PASS");
        pp.setComment(comment);
        String sid=pp.getStuId();
        User user=usr.findOne(sid);
        user.setAdditionalData("state","PROPOSAL_PASS");
        usr.save(user);
        return ppr.save(pp);
    }

    public Proposal depassProposal(String rid, String comment) {
        Proposal pp=new Proposal();
        pp=ppr.findOne(rid);
        pp.setState("PENDING");
        pp.setComment(comment);
        String sid=pp.getStuId();
        User user=usr.findOne(sid);
        user.setAdditionalData("state","PROPOSAL_PENDING");
        usr.save(user);
        return ppr.save(pp);
    }
}
